package com.zkny.zknyerp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zkny.zknyerp.mapper.TypeMapper;
import com.zkny.zknyerp.model.Type;
import com.zkny.zknyerp.service.TypeService;
import com.zkny.zknyerp.utils.Constant;
import com.zkny.zknyerp.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 用户服务接口实现类
 */
@Service
@Transactional
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    /**
     * 设备类型添加接口
     * @param tname
     * @return
     */
    public Result addType(String tname){
        Example example = new Example(Type.class);
        example.createCriteria().andEqualTo("tname",tname);
        Type type1 = typeMapper.selectOneByExample(example);
        if(!ObjectUtils.isEmpty(type1)){
            return new Result(Constant.code_1017,Constant.msg_1017);
        }

        Type type2 = new Type();
        type2.setTname(tname);
        int i = typeMapper.insert(type2);
        if(0 == i){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000);
    }


    /**
     * 类型查询接口
     */
    public Result selectType(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Type> list = typeMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(Constant.code_1002,Constant.msg_1002);
        }
        PageInfo<Type> pageInfo = new PageInfo<Type>(list);
        return new Result(Constant.code_0000,Constant.msg_0000,pageInfo);
    }

    /**
     * 输入框选项查询
     */

    public Result getType(){
        List<Type> list = typeMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(Constant.code_1002,Constant.msg_1002);
        }
        return new Result(Constant.code_0000,Constant.msg_0000,list);
    }

    /**
     * 类型数据修改接口
     */
    public Result updateType(Type type){
        int i = typeMapper.updateByPrimaryKeySelective(type);
        if(0 == i ){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000);
    }


    /**
     * 类型删除接口
     */
    public Result deleteType(Type type){
        int i = typeMapper.deleteByPrimaryKey(type);
        if(0 == i){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000);
    }

    /**
     * 根据id查询数据
     */
    public Result getId(Integer id){
        Example example = new Example(Type.class);
        example.createCriteria().andEqualTo("id",id);
        Type type= typeMapper.selectOneByExample(example);
        if(ObjectUtils.isEmpty(type)){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000,type);
    }

}
